
import { createNamespacedHelpers } from 'vuex';

export default function mixinsList(moduleName) {
    const { mapState, mapActions } = createNamespacedHelpers(moduleName);
    
    return {
        data() {
            return {
                pageData: {
                    currentPage: 1,
                    pageSize: 5,
                    // 搜索数据
                    type: 'name',
                    value: '',
                    // 会员日期
                    date:""
                },
                sliceNum : 5,
                slice : 0,
                sliceRows:[],

            }
        },
        watch: {
            pageData: {
                handler() {
                    this.getDataAsync(this.pageData)
                },
                deep: true
            },
            "pageData.pageSize"() {
                this.pageData.currentPage = 1;
                this.getDataAsync()
            },
            rows:{
                handler(){
                  this.getSliceData()
               },
               deep:true
           },
        },
        computed: {
            ...mapState(['rows', 'total', 'pages']),
        },
        created() {
            this.getDataAsync();
            setTimeout(() => {
				this.getSliceData()
			}, 500)
        },
        methods: {
            ...mapActions(['getDataAsync', 'deleteOneDataAsync', 'updateOneDataAsync', 'cancleOneDataAsync']),
           
            // 删除
            async deleteOne(_id) {
                const {data} = await this.deleteOneDataAsync({_id});
                if(data.meta.status == 200) {
                    this.getDataAsync();
                    this.$message({
                        message: "恭喜你，删除成功",
                        type: "success",
                    });
                }else {
                    this.$message.error("抱歉，删除失败");
                    console.log(data);
                }
            },
            // 充值
            async updateOne(_id) {
                const {data} = await this.updateOneDataAsync({_id, date:this.date});
                console.log(this.date);
                if(data.meta.status == 200) {
                    this.getDataAsync(this.pageData);
                    this.$message({
                        message: "恭喜你，充值会员成功",
                        type: "success",
                    });
                }else {
                    this.$message.error("抱歉，充值会员失败");
                    console.log(data);
                }
            },
            // 取消
            async cancleOne(_id) {
                console.log(_id);
                const {data} = await this.cancleOneDataAsync({_id});
                if(data.meta.status == 200) {
                    this.getDataAsync(this.pageData);
                    this.$message({
                        message: "恭喜你，取消会员成功",
                        type: "success",
                    });
                }else {
                    this.$message.error("抱歉，取消会员失败");
                    console.log(data);
                }
            }, 
            // 搜索
            onSearch() {
                this.getDataAsync({ ...this.searchData });
                this.pageData.value = '';
            },

            handleSizeChange(val) {
                this.pageData.pageSize=val
                this.sliceNum = val
                // console.log(`每页 ${val} 条`);
                // this.getSliceData()
            },
            handleCurrentChange(val) {
                this.pageData.currentPage=val
                // console.log(`当前页: ${val}`);
                this.sliceNum = val *this.pageData.pageSize 
                this.slice = (val - 1 ) *this.pageData.pageSize 
                // console.log('slice',this.slice ,);
                // console.log('sliceNum',this.sliceNum,);
                this.getSliceData()
            },
            getSliceData(){
                this.sliceRows = this.rows.slice(this.slice,this.sliceNum)
                // console.log(this.sliceRows);
            },
         

        },
    }
}